<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :keyboard="false"
    :forceRender="true"
    v-bind:prefixNo="prefixNo"
    switchHelp
    switchFullscreen
    @cancel="handleCancel"
    :id="prefixNo"
    style="top:20px;height: 95%;">
    <template slot="footer">
      <a-button @click="handleCancel">取消</a-button>
      <a-button @click="exportExcel" icon="download">导出</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row class="form-row" :gutter="24">
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="日期">
              <j-date v-decorator="['operTime', validatorRules.operTime]" :show-time="false" date-format="YYYY-MM-DD"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :lg="24" :md="24" :sm="24">
            <j-editable-table id="billModal"
              :ref="refKeys[0]"
              :loading="materialTable.loading"
              :columns="materialTable.columns"
              :dataSource="materialTable.dataSource"
              :minWidth="minWidth"
              :rowNumber="false"
              :rowSelection="true"
              :actionButton="true"
              :dragSortAndNumber="true"
              @valueChange="onValueChange"
              @added="onAdded"
              @deleted="onDeleted">
              <template #depotBatchSet>
                <a-icon type="down" @click="handleBatchSetDepot" />
              </template>
            </j-editable-table>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
import { FormTypes } from '@/utils/JEditableTableUtil'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
import { BillModalMixin } from '../mixins/BillModalMixin'
import JUpload from '@/components/jeecg/JUpload'
import JDate from '@/components/jeecg/JDate'
import { handleIntroJs } from '@/utils/util'

export default {
    name: "ShipuOutDayModal",
    mixins: [JEditableTableMixin, BillModalMixin, JeecgListMixin],
    components: {
      JUpload,
      JDate,
      VNodes: {
        functional: true,
        render: (h, ctx) => ctx.props.vnodes,
      }
    },
    data () {
      return {
        title:"操作",
        width: '1600px',
        moreStatus: false,
        showSaveButton: true,
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        visible: false,
        operTimeStr: '',
        prefixNo: 'LSCK',
        fileList:[],
        minWidth: 1100,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        refKeys: ['materialDataTable', ],
        activeKey: 'materialDataTable',
        materialTable: {
          loading: false,
          dataSource: [],
          columns: [
            { title: '菜别', key: 'type', width: '20%', type: FormTypes.normal, placeholder: '请选择${title}', options: [],
              allowSearch:true, validateRules: [{ required: true, message: '${title}不能为空' }]
            },
            { title: '菜名', key: 'shipu', width: '20%', type: FormTypes.normal, placeholder: '请选择${title}', options: [],
              allowSearch:true, validateRules: [{ required: true, message: '${title}不能为空' }]
            },
            { title: '食材名', key: 'kucun', width: '20%', type: FormTypes.normal, placeholder: '请选择${title}', options: [],
              allowSearch:true, validateRules: [{ required: true, message: '${title}不能为空' }]
            },
            { title: '数量(KG)', key: 'amount', width: '10%', type: FormTypes.normal, statistics: true,
              validateRules: [{ required: true, message: '${title}不能为空' }]
            }
          ]
        },
        confirmLoading: false,
        validatorRules:{
          operTime:{
            rules: [
              { required: true, message: '请输入日期!' }
            ]
          }
        },
        url: {
          list: '/shipu/day/getDetailList',
          add: '/shipu/addDayDetail',
          detailList: '/shipu/day/getDetailList'
        }
      }
    },
    created () {
      let realScreenWidth = window.screen.width
      this.minWidth = realScreenWidth<1500?800:1100
      this.initShipu()
      this.initKucun()
    },
    methods: {
      //调用完edit()方法之后会自动调用此方法
      editAfter() {
        this.billStatus = '0'
        this.currentSelectDepotId = ''
        this.changeColumnHide()
        if (this.action === 'add') {
          this.addInit(this.prefixNo)
          this.fileList = []
          this.$nextTick(() => {
            handleIntroJs(this.prefixNo, 1)
          })
        } else {
          this.model.operTime = this.model.operTimeStr
          this.fileList = this.model.fileName
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model, 'operTime'))
          });
        }
        //复制新增单据-初始化单号和日期
        if(this.action === 'copyAdd') {
          this.model.id = ''
          this.model.tenantId = ''
          this.copyAddInit(this.prefixNo)
        }
        // this.initSystemConfig()
        // this.initRetail()
        // this.initDepot()
        this.initShipu()
        this.initKucun()
        // this.initAccount()
      },
      //调用完view()方法之后会自动调用此方法
      viewAfter() {
        this.model.operTime = this.model.day
        this.$nextTick(() => {
          this.form.setFieldsValue({'operTime': this.model.day})
        })
        // 加载子表数据
        let params = {
          day: this.model.day
        }
        this.requestSubTableData(this.url.detailList, params, this.materialTable);
        /*this.billStatus = '0'
        this.currentSelectDepotId = ''
        this.changeColumnHide()
        if (this.action === 'add') {
          this.addInit(this.prefixNo)
          this.fileList = []
          this.$nextTick(() => {
            handleIntroJs(this.prefixNo, 1)
          })
        } else {
          this.model.operTime = this.model.operTimeStr
          this.fileList = this.model.fileName
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model, 'operTime'))
          });
        }
        //复制新增单据-初始化单号和日期
        if(this.action === 'copyAdd') {
          this.model.id = ''
          this.model.tenantId = ''
          this.copyAddInit(this.prefixNo)
        }
        // this.initSystemConfig()
        // this.initRetail()
        // this.initDepot()
        this.initShipu()
        this.initKucun()
        // this.initAccount()*/
      },
      //提交单据时整理成formData
      classifyIntoFormData(allValues) {
        let billMain = Object.assign(this.model, allValues.formValue)
        let detailArr = allValues.tablesValue[0].values
        console.log('billMain',billMain)
        console.log('detailArr',detailArr)
        if(this.model.id){
          billMain.id = this.model.id
        }
        return {
          info: JSON.stringify(billMain),
          rows: JSON.stringify(detailArr),
        }
      },
      //改变实收金额、收款金额的值
      autoChangeKucunAmount(kucunAmount) {
        this.$nextTick(() => {
          this.form.setFieldsValue({'kucunAmount':kucunAmount})
        });
      },
      exportExcel() {
        let list = []
        let head = '出库日期,当前菜品类别,当前菜品名称,当前出库物品名称,当前出库物品数量(KG)'
        for (let i = 0; i < this.materialTable.dataSource.length; i++) {
          let item = []
          let ds = this.materialTable.dataSource[i]
          item.push(this.model.operTime, ds.type, ds.shipu, ds.kucun, ds.amount)
          list.push(item)
        }
        let tip = '食堂出库记录'
        this.handleExportXlsPost('食堂出库记录', '食堂出库记录', head, tip, list)
      }
    }
  }
</script>
<style scoped>
  .sign .ant-input{
    font-size: 30px;
    font-weight:bolder;
    text-align:center;
    border-left-width:0px!important;
    border-top-width:0px!important;
    border-right-width:0px!important;
  }
</style>